package com.fa.production.service.impl;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.fa.production.domain.MdClient;
import com.fa.production.domain.Processs;
import com.fa.production.domain.Workorder;
import com.fa.production.mapper.WorkorderMapper;
import com.fa.production.service.WorkorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class WorkorderServiceImpl implements WorkorderService  {
    @Autowired
    private WorkorderMapper workorderMapper;

    @Override
    public List<Tree<String>> getlist() {
        long begin = System.currentTimeMillis();
        // 查询全部数据
        List<Workorder> nodeList = workorderMapper.selectAll();
        System.out.println(nodeList);


        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        //treeNodeConfig.setWeightKey("quantity");
        treeNodeConfig.setIdKey("workorderId");


        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getWorkorderId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    //tree.setWeight(treeNode.getQuantity());
                    // 扩展属性 ...
                    tree.putExtra("workorderCode", treeNode.getWorkorderCode());
                    tree.putExtra("workorderName", treeNode.getWorkorderName());
                    tree.putExtra("orderSource", treeNode.getOrderSource());
                    tree.putExtra("sourceCode", treeNode.getSourceCode());
                    tree.putExtra("productCode", treeNode.getProductCode());
                    tree.putExtra("productName", treeNode.getProductName());
                    tree.putExtra("productSpc", treeNode.getProductSpc());
                    tree.putExtra("unitOfMeasure", treeNode.getUnitOfMeasure());
                    tree.putExtra("quantity", treeNode.getQuantity());
                    tree.putExtra("quantityChanged", treeNode.getQuantityChanged());
                    tree.putExtra("quantityProduced", treeNode.getQuantityProduced());
                    tree.putExtra("clientCode", treeNode.getClientCode());
                    tree.putExtra("clientName", treeNode.getClientName());
                    tree.putExtra("requestDate", treeNode.getRequestDate());
                    tree.putExtra("status", treeNode.getStatus());
                });
        long end = System.currentTimeMillis();
        System.out.println("查询耗时: " + (end - begin) + " ms");
        System.out.println(treeNodes);
        return treeNodes;
    }

    @Override
    public List<Workorder> getlists(Workorder process) {
        return workorderMapper.getlist(process);
    }

    @Override
    public int deleteProcesssById(Integer workorderId) {
        List<Workorder> list =workorderMapper.selectbyId(workorderId);
        if(list.size()!=0){
            return 0;
        }
        return workorderMapper.deleteProcesssById(workorderId);
    }

    @Override
    public int insertWorkorder(Workorder workorder) {
        return workorderMapper.insertWorkorder(workorder);
    }

    @Override
    public HashMap<String, List<Object> >logTotal() {
        HashMap<String, List<Object>> logTotal = new HashMap<>();
        List<Object> logTotal1 = new ArrayList<>();
        logTotal1.add("登录日志");
        logTotal1.add("操作日志");
        logTotal.put("categories",logTotal1);
        List<Object> logTotal2 = new ArrayList<>();
        logTotal2.add(workorderMapper.logTotal1());
        logTotal2.add(workorderMapper.logTotal2());
        logTotal.put("values",logTotal2);

        return logTotal;
    }

    @Override
    public List<MdClient> getMdItemList(MdClient mdClient) {
        return workorderMapper.getMdItemList(mdClient);
    }
}
